package com.riant;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

@Configuration
public class AppConfig {
    @Value("${name:riant}")
    private String name;

    @Bean(name="dataSource")
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://192.168.0.12:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true");
        druidDataSource.setUsername(name);
        druidDataSource.setPassword("root");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Bean(name="pooledDataSource")
    @Primary
    @Scope("prototype")
    public DataSource pooledDataSource(){
        PooledDataSource pooledDataSource = new PooledDataSource();
        pooledDataSource.setUrl("jdbc:mysql://192.168.0.12:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true");
        pooledDataSource.setUsername(name);
        pooledDataSource.setPassword("root");
        pooledDataSource.setDriver("com.mysql.jdbc.Driver");
        pooledDataSource.setPoolMaximumActiveConnections(5);
        pooledDataSource.setPoolMaximumIdleConnections(10);
        pooledDataSource.setPoolTimeToWait(5000);
        return pooledDataSource;
    }
}
